package com.supcon.greendill.config;

import com.alibaba.cloud.nacos.ConditionalOnNacosDiscoveryEnabled;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.discovery.NacosWatch;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.client.CommonsClientAutoConfiguration;
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @author fukun
 * @date 2021/11/27 10:21
 * @since
 */

@Configuration
@ConditionalOnNacosDiscoveryEnabled
@AutoConfigureBefore({SimpleDiscoveryClientAutoConfiguration.class, CommonsClientAutoConfiguration.class})
public class NacosDiscoveryClientAutoConfiguration {

    @Value("${supos.tenant.id}")
    private String tenantId;

    @Bean
    @ConditionalOnMissingBean
    public NacosWatch nacosWatch(NacosDiscoveryProperties nacosDiscoveryProperties) {
        nacosDiscoveryProperties.setMetadata(new HashMap<>());
        nacosDiscoveryProperties.getMetadata().put("supfusion.tenantid", tenantId);
        return new NacosWatch(nacosDiscoveryProperties);
    }

}
